﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow.BusinessImpl;

namespace Wicresoft.UnifyShow.WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Service1”。
    public class ITSMDo : IITSMDo
    {

        public static readonly string appName = System.Configuration.ConfigurationManager.AppSettings["AppName"];
        public static readonly string empty = System.Configuration.ConfigurationManager.AppSettings["Empty"];
        public static readonly string one = System.Configuration.ConfigurationManager.AppSettings["One"];
        public static readonly string all = System.Configuration.ConfigurationManager.AppSettings["All"];

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }


        public ToDoModels GetToDos(string userid)
        {

            List<ToDoModel> list = new List<ToDoModel>();

            int count;

            User_XModel user = null;

            using (User_X user_x = new User_X())
            {
                user = user_x.GetUserByPeopleNo(userid.Trim());
            }

            if (user != null)
            {

                foreach (var item in ToDoHelper.GetListToDo(user.Name, out count))
                {

                    list.Add(new ToDoModel()
                {
                    AppName = appName,
                    Extends = new ExtendModel()
                    {
                        extend1 = item.AssignGroup,
                        extend2 = item.Assigner,
                        extend3 = item.Status,
                        extend4 = item.RequestID,
                        extend5 = item.Submitter
                    },
                    Time = DateTime.Parse(item.CreateDate),
                    Title = item.Description,
                    Url = "http://selfservice.fj-tobacco.com:8098/ITSMDo/Do?types=" + item.TypeS + "&operation=" + item.Operation + "&requstID=" + item.RequestID
                });
                }

                return new ToDoModels() { ToDos = list };
            }

            return null;


        }


        public ToDoModel GetToDo(string username)
        {
            return new ToDoModel()
            {
                AppName = appName,
                Extends = new ExtendModel()
                {
                    extend1 = "1",
                    extend2 = "2"
                },
                Time = DateTime.Parse(DateTime.Now.ToString()),
                Title = "Title",
                Url = "http"
            };
        }


        public ToDoModels GetToDosTest(string username)
        {
            List<ToDoModel> list = new List<ToDoModel>();

            string appName = System.Configuration.ConfigurationManager.AppSettings["AppName"];

            for (int i = 0; i < 50; i++)
            {
                list.Add(new ToDoModel()
            {
                AppName = appName,
                Extends = new ExtendModel()
                {
                    extend1 = "1",
                    extend2 = "2"
                },
                Time = DateTime.Parse(DateTime.Now.ToString()),
                Title = "Title " + i,
                Url = "http://selfservice.fj-tobacco.com:8098/ITSMDo/Do?types=ite"
            });
            }

            return new ToDoModels() { ToDos = list };
        }


        public string getUserXML(string strUserID, string strType)
        {
            if (string.IsNullOrWhiteSpace(strUserID) || string.IsNullOrWhiteSpace(strType)) return empty;
            if (strType == "ToDo")
            {

                string appName = System.Configuration.ConfigurationManager.AppSettings["AppName"];

                int count;

                User_XModel user = null;

                using (User_X user_x = new User_X())
                {
                    user = user_x.GetUserByPeopleNo(strUserID.Trim());
                }

                if (user != null)
                {

                    var todos = ToDoHelper.GetListToDo(user.Name, out count);

                    if (count == 0)
                        return empty;

                    StringBuilder sb = new StringBuilder();

                    foreach (var item in todos)
                    {
                        sb.AppendLine(string.Format(one, item.Description, DateTime.Parse(item.CreateDate).ToString(),
                            appName, "http://selfservice.fj-tobacco.com:8098/ITSMDo/Do?types=" + System.Web.HttpUtility.UrlEncode( item.TypeS) + "&operation=" + System.Web.HttpUtility.UrlEncode(item.Operation) + "&requstID=" + System.Web.HttpUtility.UrlEncode(item.RequestID),
                            item.AssignGroup, item.Assigner, item.Status, item.RequestID, item.Submitter));
                    }

                    return string.Format(all, count, sb.ToString());
                }
            }
            return empty;


        }

    }
}
